package com.ygqh.baby.service.impl;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.model.wdt.qyb.PushOrderToWdtResult;
import com.ygqh.baby.model.wdt.qyb.WdtQybResult;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.po.YgOrderLog;
import com.ygqh.baby.service.YgOrderLogService;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.service.YgThirdPartyService;
import com.ygqh.baby.service.wdtqyb.api.WdtQybApi;
import com.ygqh.baby.service.wdtqyb.exception.WdtException;
import com.ygqh.baby.utils.ExceptionUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

@Service
public class YgThirdPartyServiceImpl implements YgThirdPartyService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    private static final ExecutorService threadPool = Executors.newFixedThreadPool(20);

    @Autowired
    private YgOrderService ygOrderService;
    @Autowired
    private YgOrderLogService ygOrderLogService;
    @Autowired
    private WdtQybApi wdtQybApi;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void saveYgOrderList(String shopNo, List<YgOrder> orders) {
        if (CollectionUtils.isNotEmpty(orders)) {
            ygOrderService.saveOrderList(orders);
            List<Long> orderNos = orders.stream().map(YgOrder::getOrderId).collect(Collectors.toList());
            // 订单批量推送到旺店通
            tradePushOrderListToWDTQyb(shopNo, orderNos);
        }
    }

    private void tradePushOrderListToWDTQyb(String shopNo, List<Long> orderNos) {
        threadPool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    List<YgOrder> orders = ygOrderService.findOrderDetailByOrderNoList(orderNos);

                    WdtQybResult wdtQybResult = wdtQybApi.tradePushBatchByShop(shopNo, orders);

                    List<PushOrderToWdtResult> pushSuccessList = wdtQybResult.getPushSuccessList();
                    logger.info("tradePushOrderListToWDTQyb : result=【{}】", JSON.toJSONString(wdtQybResult));
                    ygOrderService.updateWdtCodeBatch(pushSuccessList);
                    List<YgOrderLog> orderLogs = new ArrayList<>();
                    pushSuccessList.forEach(order -> {
                        YgOrderLog ygOrderLog = new YgOrderLog();
                        ygOrderLog.setOrderId(Long.parseLong(order.getOrderCode()));
                        ygOrderLog.setOrderStatus(OrderStatus.WaitingSend);
                        ygOrderLog.setCreateBy("system");
                        ygOrderLog.setRemark("第三方主订单推送到旺店通成功 ;");
                        ygOrderLog.setCreateTime(new Date());
                        orderLogs.add(ygOrderLog);
                    });
                    ygOrderLogService.addOrderLogBatch(orderLogs);
                } catch (WdtException e) {
                    logger.error("第三方订单向旺店通批量推送发生错误。orderNos=" + orderNos.toString());
                    logger.error(ExceptionUtil.getExceptionMsg(e));
                }
            }

        });
    }

}
